#ifndef __PR2000_H__
#define __PR2000_H__

#include "pr2000_support.h"
#include "pr2000_table.h"
#include "pr2000_drvcommon.h"
#include "pr2000_user_config.h"
#include "typedef.h"
//#include "sys.h"

#if defined(__HOST_LINUX_SYSTEM__)
#undef TRUE
#define TRUE  		(1)
#undef FALSE
#define FALSE  		(0)
//#undef ENABLE
//#define ENABLE 		(1)
//#undef DISABLE
//#define DISABLE		(0)
#endif //defined(__HOST_LINUX_SYSTEM__)
#undef START
#define START  		(1)
#undef STOP
#define STOP  		(0)
#undef HIGH
#define HIGH  		(1)
#undef LOW
#define LOW  		(0)

#define COMMA ,

#ifndef	_VER_PR2000
#define _VER_PR2000 "0_95"
#endif //_VER_PR2000

#if defined(__HOST_LINUX_SYSTEM__)
#include <linux/spinlock.h>
#include <linux/wait.h>

#ifndef NULL
#define NULL  		(0)
#endif
#define _SET_BIT(bit, pData) (set_bit(bit, pData))
#define _CLEAR_BIT(bit, pData) (clear_bit(bit, pData))
#define _TEST_BIT(bit, pData) (test_bit(bit, pData))
#define WAKE_UP_INTERRUPTIBLE(pData) (wake_up_interruptible(pData))
#define DO_DIV(x,y) (do_div(x,y))
#define SPIN_LOCK_INIT(pData) spin_lock_init(pData)
#define SPIN_LOCK_IRQSAVE(pData, flag) spin_lock_irqsave(pData, flag)
#define SPIN_UNLOCK_IRQRESTORE(pData, flag) spin_unlock_irqrestore(pData, flag)
typedef spinlock_t SPINLOCK_T;
typedef	wait_queue_head_t WAITQHEAD_T;

#define SUPPORT_PROC_SYSTEM //support proc system for linux debugging.
#define ZALLOC(pMem, flag) kzalloc(pMem, flag)
#define FREE(pMem) kfree(pMem)

#ifdef I2C_INTF_LINUX_ADAPTER_DRIVER /* linux native i2c adapter drvier */
typedef struct  {
        struct i2c_client *pr2000_i2c_client;
}_pr2000_i2c_data;
#endif // I2C_INTF_LINUX_ADAPTER_DRIVER


#elif defined(__HOST_MICOM_SYSTEM__)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//#include "user_func.h"


#ifndef NULL
#define NULL  		(0)
#endif
#define _SET_BIT(bit, pData) (*pData|=(1<<bit))
#define _CLEAR_BIT(bit, pData) (*pData&=~(1<<bit))
#define _TEST_BIT(bit, pData) ((*pData&(1<<bit))?1:0)
#define WAKE_UP_INTERRUPTIBLE(pData) (*pData=1)
#define DO_DIV(x,y) ((uint64_t)x/(uint64_t)y)
#define SPIN_LOCK_INIT(pData) (*pData=0)
#define SPIN_LOCK_IRQSAVE(pData, flag) {while(*pData==1);*pData=1;}
#define SPIN_UNLOCK_IRQRESTORE(pData, flag) (*pData=0)
typedef volatile int SPINLOCK_T;
typedef	volatile int WAITQHEAD_T;
#define ZALLOC(pMem) malloc(pMem)
#define FREE(pMem) free(pMem)

#undef SUPPORT_PROC_SYSTEM //support proc system for linux debugging.

/* define value */
#define POLLIN          0x0001
#define POLLPRI         0x0002
#define POLLOUT         0x0004
#define POLLERR         0x0008
#define POLLHUP         0x0010
#define POLLNVAL        0x0020
/* The rest seem to be more-or-less nonstandard. Check them! */
#define POLLRDNORM      0x0040
#define POLLRDBAND      0x0080
#define POLLWRNORM      0x0100
#define POLLWRBAND      0x0200
#define POLLMSG         0x0400
#define POLLREMOVE      0x1000
#define POLLRDHUP       0x2000

#endif // __HOST_LINUX_SYSTEM__

#define MAX_CNT_TUNN_CHROMALOCK              (10)                              

////////////////////////////////////////////////////////////////////////
typedef struct {
	uint8_t			i2cSlvAddr;
	int 			minor;
	int			irq;
	uint16_t		chipID;
	uint8_t			u8RevID;
	int			i2cRW_Verify;

	WAITQHEAD_T 		wqPoll;

	_stPR2000_Isr		stPR2000_Isr;
	_stPrRxMode 		stPrRxMode;
#ifdef SUPPORT_UTC
	_stUTCCmd		stUTCRecvCmd;
#endif // SUPPORT_UTC
#ifdef SUPPORT_CEQ
#ifdef SUPPORT_PROC_SYSTEM //support proc system for linux debugging.
	_stCEQInfo		stCEQInfo;
#endif // SUPPORT_PROC_SYSTEM //support proc system for linux debugging.
#endif // SUPPORT_CEQ
	//////////////////////////////////////////////
}_drvHost;
extern _drvHost *gpDrvHost[MAX_PR2000_CHIPCNT];
extern _drvHost gDrvHost[MAX_PR2000_CHIPCNT];

#ifdef SUPPORT_REG_SAVEDUMP //support pr2000 register save dump.
typedef struct {
	uint8_t	i2cSlvAddr;
	uint8_t	curPage;	 //current page.
	_stPR2000_Table_Common regTbl[3][0xFF]; //0,1,2page 0x00~0xFE
}_stRegSaveDump;
extern _stRegSaveDump *gpRegSaveDump[MAX_PR2000_CHIPCNT];
#endif // SUPPORT_REG_SAVEDUMP //support pr2000 register save dump.

extern int gFindPr2000SlvAddrCnt;
int PR2000_SetPage(const int fd, const uint8_t slvAddr, int page);
int PR2000_Read(const int fd, const uint8_t slvAddr, const uint8_t addr, uint8_t *pData);
int PR2000_Write(const int fd, const uint8_t slvAddr, const uint8_t addr, uint8_t Data);
int PR2000_PageRead(const int fd, const uint8_t slvAddr, const int page, const uint8_t addr, uint8_t *pData);
int PR2000_PageWrite(const int fd, const uint8_t slvAddr, const int page, const uint8_t addr, uint8_t Data);
int PR2000_PageReadMaskBit(const int fd, const uint8_t slvAddr, const int page, const uint8_t addr, const uint8_t maskBit, uint8_t *pData);
int PR2000_PageWriteMaskBit(const int fd, const uint8_t slvAddr, const int page, const uint8_t addr, const uint8_t maskBit, const uint8_t Data);
int PR2000_PageReadBurst(const int fd, const uint8_t slvAddr, const int page, const uint8_t addr, const uint8_t length, uint8_t *pData);
int PR2000_PageWriteBurst(const int fd, const uint8_t slvAddr, const int page, const uint8_t addr, const uint8_t length, uint8_t *pData);

//////////////////////////////////////////////////////////////////
#if defined(__HOST_LINUX_SYSTEM__)
#define Print(string, args) {printk("[PixelPlus]dbg>(%s:%d) ",__FILE__, __LINE__);printk(string,args); }
#define Error(string, args) {printk("%c[0;31;47m [PixelPlus] ### ERR ### :(%s:%d):",27,__FUNCTION__, __LINE__);printk(string,args);printk("%c[0m 0;31m\n",27);}
#ifdef SUPPORT_DBG_PRINT
#define Dbg(string, args) {printk("[PixelPlus]dbg>(%s:%d) ",__FILE__, __LINE__);printk(string,args);}
#else
#define Dbg(string, args)
#endif
#define PrintString(string) {printk("[PixelPlus]dbg>(%s:%d) ",__FILE__, __LINE__);printk(string); }
#define ErrorString(string) {printk("%c[0;31;47m [PixelPlus] ### ERR ### :(%s:%d):",27,__FUNCTION__, __LINE__);printk(string);printk("%c[0m 0;31m\n",27);}
#ifdef SUPPORT_DBG_PRINT
#define DbgString(string) {printk("[PixelPlus]dbg>(%s:%d) ",__FILE__, __LINE__);printk(string);}
#else
#define DbgString(string)
#endif
#define PRINTF	printk

#ifdef SUPPORT_DBG_PRINT
#define Print(string, args) {PRINTF("[PixelPlus] ");PRINTF(string,args); PRINTF("\r");}
#define Error(string, args) {PRINTF("%c[0;31;47m [PixelPlus] ### ERR ### :",27);PRINTF(string,args);PRINTF("%c[0m 0;31m\r",27);}

#define Dbg(string, args) //{PRINTF("[PixelPlus]dbg> ");PRINTF(string,args);PRINTF("\r");}
#define AppPrint(string, args) {PRINTF(string,args);}
#define AppPrintString(string) {PRINTF(string);}
#define AppError(string, args) {PRINTF("%c[0;31;47m ERR[%s(%bx)]: ",27,__FILE__,__LINE__);PRINTF(string,args);PRINTF("%c[0m 0;31m\r\n",27);}
#define AppErrorString(string) {PRINTF("%c[0;31;47m ERR[%s(%bx)]: ",27,__FILE__,__LINE__);PRINTF(string);PRINTF("%c[0m 0;31m\r\n",27);}

#define ErrorString(string) {PRINTF("%c[0;31;47m [PixelPlus] ### ERR ### :",27);PRINTF(string);PRINTF("%c[0m 0;31m\r",27);}

#define PrintString(string) {PRINTF("[PixelPlus] ");PRINTF(string); PRINTF("\r");}

#else
#define Print(string, args)
#define Error(string, args)

#define Dbg(string, args)
#define AppPrint(string, args)  {PRINTF(string,args);}
#define AppPrintString(string)
#define AppError(string, args)
#define AppErrorString(string)
#define ErrorString(string)

#define PrintString(string)

#endif


#elif defined(__HOST_MICOM_SYSTEM__)

#if 0
int pr2000_module_init(void);
void pr2000_module_exit(void);
int IrqHandler(void);
uint32_t pr2000_poll(uint32_t waitMsec);
#endif

#ifdef SUPPORT_REG_SAVEDUMP //support pr2000 register save dump.
extern _stRegSaveDump gRegSaveDump[MAX_PR2000_CHIPCNT];
#endif // SUPPORT_REG_SAVEDUMP //support pr2000 register save dump.


//#define Print(string, args)
//#define Error(string, args)
#ifdef SUPPORT_DBG_PRINT
#define Print(string, args) {PRINTF("[PixelPlus] ");PRINTF(string,args); PRINTF("\r");}
#define Error(string, args) {PRINTF("%c[0;31;47m [PixelPlus] ### ERR ### :",27);PRINTF(string,args);PRINTF("%c[0m 0;31m\r",27);}

#define Dbg(string, args) //{PRINTF("[PixelPlus]dbg> ");PRINTF(string,args);PRINTF("\r");}
#define AppPrint(string, args) {PRINTF(string,args);}
#define AppPrintString(string) {PRINTF(string);}
#define AppError(string, args) {PRINTF("%c[0;31;47m ERR[%s(%bx)]: ",27,__FILE__,__LINE__);PRINTF(string,args);PRINTF("%c[0m 0;31m\r\n",27);}
#define AppErrorString(string) {PRINTF("%c[0;31;47m ERR[%s(%bx)]: ",27,__FILE__,__LINE__);PRINTF(string);PRINTF("%c[0m 0;31m\r\n",27);}

#define ErrorString(string) {PRINTF("%c[0;31;47m [PixelPlus] ### ERR ### :",27);PRINTF(string);PRINTF("%c[0m 0;31m\r",27);}

#define PrintString(string) {PRINTF("[PixelPlus] ");PRINTF(string); PRINTF("\r");}

#else
#define Print(string, args)
#define Error(string, args)

#define Dbg(string, args)
#define AppPrint(string, args)  {PRINTF(string,args);}
#define AppPrintString(string)
#define AppError(string, args)
#define AppErrorString(string)
#define ErrorString(string)

#define PrintString(string)

#endif
//#define PrintString(string)
//#define ErrorString(string)
#ifdef SUPPORT_DBG_PRINT
#define DbgString(string) {PRINTF("[PixelPlus]dbg> ");PRINTF(string);PRINTF("\r");}
#else
#define DbgString(string)
#endif



#endif //defined(__HOST_LINUX_SYSTEM__)

#endif /* __PR2000_H__ */


